<?php

class CommonApp extends Controller{

    public function _initialize() {
    	C('SHOW_PAGE_TRACE',(My::arr2var($_GET,'debug')=='true' OR C('SHOW_PAGE_TRACE')==true)?true:false);//debug调试
		//上一页面url
		define('URL_', (My::user('URL_'))?My::user('URL_'):empty($_SERVER['HTTP_REFERER'])?"":$_SERVER['HTTP_REFERER']);
    	self::setcookie();//设置相关cookie变量供前端用
	}
	
	/**
	 * 设置相关cookie变量供前端用
	 */
	private function setcookie(){
		setcookie("__ROOT__", __ROOT__,null,__ROOT__);
		setcookie("__PROJECT__", __PROJECT__,null,'/');
	}

	/**
	 * 跳转函数(兼容函数)
	 * by huanran.zhao
	 */
	function ajaxReturn($statusCode, $message, $callbackType = '', $forwardUrl = '', $data='') {
		//404页面,使用方法$this->ajaxReturn(404,'找不到页面');
		if($statusCode==404){
			$this->assign('title',$message);
			$this->display('404');
			exit();
		}
		//ajaxsubmit方式提交的表单(表单验证方式)
		if(My::is_ajax()){
			$jsonData = array(
								'statusCode' => $statusCode,
								'message' => $message,
								'forwardUrl' => $forwardUrl,
								'data' => $data
							 );
			exit(json_encode($jsonData));
		}
		//正常方式页面跳转
		else{
			if(isset($_GET['URL'])) $URL=$_GET['URL'];//将get赋值给post
			if(isset($_POST['URL_'])) $URL_=$_POST['URL_'];//将get赋值给post
			//有传参时,强行按这个跳转url=$url
			if(!empty($url)) $URL=$URL_=$url;
			if(My::user('URL_')) $URL_=My::user('URL_');
			//默认
			if(empty($URL)) $URL = URL;
			if(empty($URL_)) $URL_ = URL_;
			
			$this->assign("text",$message);
			$this->assign("time",2);
			if($statusCode==200){
				unset($_SESSION['URL_']);
				$this->assign("url",$URL_);//成功:返回上一页面URL_
			}else{
				$_SESSION['URL_'] = $URL_;
				$this->assign("url",$URL);//失败:返回上一页面
			}
			//debug调试控件
			MagicFactory::getInstance('PageTrace')->run();
			//输入json内容
			exit(json_encode(array('statusCode'=>$statusCode,'message'=>$message)));
		}
	}
    
}